/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controler.controler;


import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.LinkedList;

import java.util.List;
import modele.DAO;
import modele.Medecin;

/**
 *
 * @author sio
 */
public class MedActivity extends ListActivity{
    
    protected void onListItemClick(ListView l, View v, int position, long id){
        
        Medecin m = (Medecin) l.getItemAtPosition(position);
        final String contact = m.getTel();
        final String adresse = m.getAdresse().replace(" ","+");
        
        AlertDialog builder = new AlertDialog.Builder(this).create();
            builder.setButton("téléphoner", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface di, int i) {
               Intent callIntent = new Intent(Intent.ACTION_CALL);
               callIntent.setData(Uri.parse("tel:"+contact));
               callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               startActivity(callIntent);
            }
        });
            
        builder.setMessage("Appelez ou envoyez un SMS");
        final EditText message = new EditText(this);
        
        builder.setView(message);
        builder.setButton2("Envoyer un SMS", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface di, int i) {
              SmsManager smsManager = SmsManager.getDefault();
              smsManager.sendTextMessage(contact, null, message.getText().toString(),null,null);
              Toast.makeText(getApplicationContext(), "Votre SMS a bien été envoyé.", Toast.LENGTH_LONG).show();
            }
        });
        
        builder.setButton3("Voir le plan d'accès", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface di, int i) {
                Intent plan = new Intent(getApplicationContext(), PlanActivity.class);
                plan.putExtra("adresse", adresse.toString());
                startActivity(plan);
               
            }

            
        });
        
        builder.show();
        
    }
    
    @Override
     public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        
        setContentView(R.layout.med);
        Intent inter = getIntent();
        String dep = inter.getStringExtra("leDep");
        final List <Medecin> lesMedsParDeps = DAO.getLesMedsParDep(dep);
        MedAdapter adapter = new MedAdapter(lesMedsParDeps, this);
        setListAdapter(adapter);
        
        Button filtre = (Button) findViewById(R.id.rechercheAvancee);
        filtre.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                EditText recherche = (EditText) findViewById(R.id.filtre);
                List <Medecin> lesMedsFiltre = new LinkedList <Medecin>();
                for (Medecin m : lesMedsParDeps){
                    if(m.getNom().startsWith(recherche.getText().toString())){
                        lesMedsFiltre.add(m);
                    }
                }
                MedAdapter adapter = new MedAdapter(lesMedsFiltre, getApplicationContext());
                setListAdapter(adapter);
            }
        });
     }
}
